package com.itheima.mall.api.sms;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.sms.SmsFlashPromotionMapper;
import com.itheima.mall.pojo.sms.SmsFlashPromotion;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.R;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
@DubboService
public class SmsFlashPromotionServiceApiImpl implements SmsFlashPromotionServiceApi {
   @Autowired
    SmsFlashPromotionMapper smsFlashPromotionMapper;
    @Override
    public PageResult<SmsFlashPromotion> getList(String keyword, int pageNum, int pageSize) {

        Page pageInfo=new Page(pageNum,pageSize);
        LambdaQueryWrapper<SmsFlashPromotion> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(keyword!=null,SmsFlashPromotion::getTitle,keyword);

        Page page = smsFlashPromotionMapper.selectPage(pageInfo, queryWrapper);
        PageResult<SmsFlashPromotion> pageResult=new PageResult<>();
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        pageResult.setList(page.getRecords());
        pageResult.setPageNum(Convert.toInt(page.getSize()));
        pageResult.setPageSize(Convert.toInt(page.getSize()));
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        return pageResult;
    }

    @Override
    public void updateStatus(int id1,int status) {
        //LambdaQueryWrapper<SmsFlashPromotion> queryWrapper=new LambdaQueryWrapper<>();
        //queryWrapper.eq(SmsFlashPromotion::getStatus,id);
        Long id= Convert.toLong(id1);
        SmsFlashPromotion smsFlashPromotion=new SmsFlashPromotion();
        smsFlashPromotion.setStatus(status);
        smsFlashPromotion.setId(id);
        smsFlashPromotionMapper.updateById(smsFlashPromotion);
    }

    @Override
    public void editActivity(SmsFlashPromotion smsFlashPromotion, Long id) {
        smsFlashPromotion.setId(id);
        smsFlashPromotionMapper.updateById(smsFlashPromotion);
    }

    //添加活动
    @Override
    public void addActivity(SmsFlashPromotion smsFlashPromotion) {
        smsFlashPromotion.setCreateTime(Convert.toDate(System.currentTimeMillis()));
        smsFlashPromotionMapper.insert(smsFlashPromotion);
    }

    //删除活动
    @Override
    public void deleteActivity(Integer id) {
        smsFlashPromotionMapper.deleteById(id);
    }
}
